package com.android.huimeiqupin.fragment.group;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.huimeiqupin.R;
import com.android.huimeiqupin.fragment.BaseLazyFragment;
import com.android.huimeiqupin.model.group.AdPictureData;
import com.android.huimeiqupin.model.group.AdPictureDataCallback;
import com.android.huimeiqupin.model.group.CrowdFundingTaste;
import com.android.huimeiqupin.model.group.CrowdFundingTasteCallback;
import com.android.huimeiqupin.utils.ConstantsUrl;
import com.android.huimeiqupin.utils.HttpUtils;
import com.android.huimeiqupin.utils.ObjectUtils;
import com.android.huimeiqupin.view.group.ViewPagerIndicator;
import com.android.huimeiqupin.view.recycleview.CustomLoadMoreView;
import com.android.huimeiqupin.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 新品预售
 */

public class Fragment_CFundingTaste extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.listView)
    CustomRecycleView listview;
    private ViewPager bannerpager;
    private ViewPagerIndicator viewPagerIndicator1;
    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private LinearLayoutManager mLayoutManager;
    private AdaptList adapter;
    private ArrayList<AdPictureData.DataBean.AdBean> Bannerlist = new ArrayList<>();
    private ArrayList<CrowdFundingTaste.DataBean.ListBean> listdata = new ArrayList<>();
    private int page = 1;
    private String sort;

    public static Fragment_CFundingTaste newInstance(String need_id) {
        Fragment_CFundingTaste fragment = new Fragment_CFundingTaste();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, need_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            sort = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_cfundingtaste_list, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetAds();
        GetCfundingTaste();
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        // GetAds();
        GetCfundingTaste();
    }

    @Override
    public void onStart() {
        super.onStart();
        viewPagerIndicator1.setVisible(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        viewPagerIndicator1.setVisible(false);
    }

    private void initUI() {
        View fragment_head = inflater.inflate(R.layout.colonelfree_head_banner, null, false);
        bannerpager = (ViewPager) fragment_head.findViewById(R.id.bannerpager_fragment);
        viewPagerIndicator1 = (ViewPagerIndicator) fragment_head.findViewById(R.id.viewPagerIndicator1);
        viewPagerIndicator1.autoScroll(getFragmentManager(), bannerpager, 4);
        adapter = new AdaptList(mcontext, listdata, "0");
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, listview);
        mLayoutManager = new LinearLayoutManager(mcontext);
        adapter.addHeaderView(fragment_head);
        listview.setLayoutManager(mLayoutManager);
        listview.setAdapter(adapter);
        // ta_ryview.GetToTop((ImageView) view.findViewById(R.id.top), 0);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, int position) {
                switch (view.getId()) {
                    case R.id.main_list_item_go_kai:
                        ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
                        break;
                }
                return false;
            }
        });
    }

    public void GetAds() {
        String address = ConstantsUrl.domain + ConstantsUrl.ADVERTISEPICTURE;
        HashMap<String, String> map = new HashMap<>();
        map.put("typeid", "14");
        HttpUtils.get(address, map, new AdPictureDataCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(AdPictureData response, int id) {
                if (response == null) {
                    return;
                }
                Bannerlist.clear();
                AdPictureData.DataBean data = response.getData();
                if (data != null && ObjectUtils.listHasData(data.getAd())) {
                    Bannerlist.addAll(data.getAd());
                    viewPagerIndicator1.setViewPager4(Bannerlist);
                    viewPagerIndicator1.setNotifyDataSetChanged();
                }
            }
        });
    }

    private void GetCfundingTaste() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.CFUNDINGTASTE;
            HashMap<String, String> map = new HashMap<>();
            map.put("page", page + "");
            map.put("sort", sort);
            map.put("order", "goods_id");

            HttpUtils.get(address, map, new CrowdFundingTasteCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(CrowdFundingTaste response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        CrowdFundingTaste.DataBean data = response.getData();
                        if (data == null || !ObjectUtils.listHasData(data.getList())) {
                            if (page == 1) {
                                noData.setVisibility(View.VISIBLE);
                            } else {
                                adapter.loadMoreEnd();
                            }
                            return;
                        }
                        List<CrowdFundingTaste.DataBean.ListBean> list = data.getList();
                        if (page == 1) {
                            listdata.clear();
                            listdata.addAll(list);
                            adapter.setNewData(list);
                            listview.setAdapter(adapter);
                        } else {
                            listdata.addAll(list);
                            adapter.addData(list);
                            adapter.loadMoreComplete();
                        }
                    } else {
                        if (page == 1) {
                            noData.setVisibility(View.VISIBLE);
                        } else {
                            adapter.loadMoreFail();
                            ObjectUtils.toast(mcontext, response.getMsg());
                        }
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }

    public class AdaptList extends BaseMultiItemQuickAdapter<CrowdFundingTaste.DataBean.ListBean, BaseViewHolder> {

        public AdaptList(Context context, ArrayList<CrowdFundingTaste.DataBean.ListBean> data, String type) {
            super(data);
            addItemType(ConstantsUrl.THEMATICGOODSLISTItemTYPE, R.layout.item_list_colonelfree);
        }

        @Override
        protected void convert(BaseViewHolder holder, CrowdFundingTaste.DataBean.ListBean mainlistdata) {
            switch (holder.getItemViewType()) {
                case ConstantsUrl.THEMATICGOODSLISTItemTYPE:
                    ((TextView) holder.getView(R.id.item_list_sort_aa_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    Float discount = Float.parseFloat(mainlistdata.getTeam_price()) / Float.parseFloat(mainlistdata.getPrice());
                    holder.setText(R.id.main_list_item_count, new DecimalFormat(".0").format(discount) + "折");
                    holder.setText(R.id.main_list_item_tuannumber, mainlistdata.getTeam_num() + "人团");
                    holder.setText(R.id.ll_start_soon_num, mainlistdata.getTeam_num() + "人团");
                    holder.setText(R.id.ll_free_num, mainlistdata.getTeam_num() + "人团");
                    int sell = mainlistdata.getSell();
                    if (sell > 0) {
                        holder.setText(R.id.main_list_item_alltuannumber2, "已团" + sell + "件");
                    }
                    ObjectUtils.photo2(mContext, mainlistdata.getImg_src(), ((ImageView) holder.getView(R.id.main_list_item_image)));
                    holder.setText(R.id.main_list_item_title, "                     " + mainlistdata.getName());
                    holder.setText(R.id.title_type, "新品预售");
                    String price1 = mainlistdata.getTeam_price();
                    SpannableString msp = new SpannableString("￥" + price1);
                    msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    holder.setText(R.id.main_list_item_money_after, msp);
                    break;
            }
        }
    }

}
